<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>编辑原始需求</title>
    <link href="../css/list-detial.css" type="text/css" rel="stylesheet"/>
</head>
<body>


<!--header-->
<header id="header" class="header">
    <div class="header-info left"><i></i><a onclick="toNedDetail()" href="javascript:">原始需求详情</a>&nbsp;>&nbsp;编辑原始需求</div>
    <div class="right-user right">
         <div class="clear t" >             <div id="headusername" class="role"></div>             <div class="icon"></div>         </div>
        <div class="head-down">
            <ul><li><a href="edit-pwd.html">修改密码</a></li><li><a onclick="loginOut()">退出系统</a></li></ul></div><div>
    </div>
    </div>
</header>
<!--menu-->
<aside id="aside" class="aside">
    <div class="logo">运营平台</div>
    <ul class="menu" id="asidemenu">
    </ul>
</aside>

<!--content-->
<div class="content">
    <div class="page-inner">
        <div class="commit-new-item"><a  onclick="creatShare()" class="btn" style="width: 100px">创建相关需求&nbsp;<b>+</b></a>&emsp;<a href="javascript:" onclick="creatShare()" style="width: 100px" class="btn">关联已有需求&nbsp;<b>+</b></a></div>
        <div class="page-content">
            <div class="table-head clear"><div class="left document-name" id="document-name"><i class="bug-i"></i></div><div class="right edit"><a class="sure" onclick="returnList()">取消</a><a onclick="commitEdit()" class="sure">保存</a></div></div>
            <div class="table-cont">
                <table class="content-table">
                    <tr>
                        <td><span class="red-start">*</span>需求名称：</td>
                        <td colspan="2"><input value="" id="needName" /><a class="error-msg">请填写需求名称！</a></td>

                    </tr>
                    <tr>
                        <td><span class="red-start">*</span>来源：</td>
                        <td>
                            <!--<select id="needSource" onchange="_API.getComeSource('needSide',false,true,$(this).val(),'needSide')"></select>-->
                            <input type="hidden" id="needSource"/>
                            <div class="exist-source clear" style="display: block" id="existSourceBox">
                            <div class="left" id="originalSourceName"></div><a class="left edit-source-btn" id="goEditSource">修改</a>
                            </div>
                            <div class="search-box clear" style="display: none" id="searchBox">

                                <input class="left" placeholder="输入至少两个字" />
                                <a class="left edit-source-btn" id="goSearchBtn">搜索</a>
                                <a class="left edit-source-btn" id="giveUp">放弃</a>
                                <select class="left" id="keyMatchSelect" onchange="_API.getComeSource('needSide',false,true,$(this).val())"><option>请输入关键词</option></select>
                            </div>
                        </td>
                        <td><span class="red-start">*</span>需求方：</td>
                        <td><select id="needSide"><option>请选择</option></select><span class="red-start" style="color: #4cae4c">先选择来源</span></td>
                    </tr>
                    <tr>
                        <td><span class="red-start">*</span>类别：</td>
                        <td><select id="typeList"></select></td>
                        <td><span class="red-start">*</span>产品:</td>
                        <td><select id="productList" onchange="_API.getFunctionList('functionList',false,false,'productList')"></select></td>
                    </tr>
                    <tr>
                        <td><span class="red-start">*</span>使用平台：</td>
                        <td><select id="platformList"></select></td>
                        <td><span class="red-start">*</span>功能模块：</td>
                        <td><select id="functionList"></select><span class="red-start" style="color: #4cae4c">先选择产品</span></td>
                    </tr>

                    <tr>
                        <td><span class="red-start">*</span>紧急程度：</td>
                        <td><select id="levelList"></select></td>
                        <td class="reliableTd" style="visibility: hidden;"><span class="red-start">*</span>可靠度：</td>
                        <td class="reliableTd" style="visibility: hidden"><select id="reliableList"></select></td>
                    </tr>
                    <tr class="area">
                        <td><span class="red-start">*</span>需求场景：</td>
                        <td colspan="3">
                            <textarea id="scenario"></textarea>
                        </td>
                    </tr>

                    <tr class="area">
                        <td>现有方案：</td>
                        <td colspan="3">
                            <textarea id="nowSolution"></textarea>
                        </td>
                    </tr>
                    <tr class="area">
                        <td>建议方案：</td>
                        <td colspan="3">
                            <textarea id="sugSolution"></textarea>
                        </td>
                    </tr>
                    <tr class="area">
                        <td>附件：</td>
                        <td colspan="3">
                            <a href="javascript:;" class="a-upload" id="fileUpload"><input id="file" value="请选择" type="file"/>点击这里上传</a>
                            <p class="format">支持的格式有：JPG，PNG，PDF，PPT，XLSX，XLS，CSV，DOCX，DOC，ZIP</p>                             <p class="format">文件名不能包含*、#等特殊符号</p>
                            <ul class="addAttachmentList" id="addAttachmentList">
                            </ul>
                        </td>
                    </tr>
                </table>
            </div>

            <div class="table-head clear"><div class="left document-name"><i class="bug-i"></i>相关产品需求</div></div>
            <div class="table-cont">
                <table class="relative-list">
                    <thead>
                        <tr>
                            <th>产品名称</th>
                            <th>功能模块</th>
                            <th>需求名称</th>
                            <th>优先级</th>
                            <th>处理状态</th>
                            <th>操作</th>
                        </tr>
                    </thead>
                    <tbody id="relativeList">
                       
                    </tbody>
                    
                </table>
            </div>
        </div>
    </div>
</div>
<script src="../js/jquery.min.js"></script>
<script src="../js/jquery.cookie.js"></script>
<script src="../js/common.js"></script>

<script type="text/javascript">
    var _id = getUrlId();
    function returnList(){
        location.href = 'pm-need-detail-mems.html?id='+_id;
    }
    function toNedDetail() {
        location.href = 'pm-need-detail-mems.html?id='+getUrlId();
    }
    //权限控制
    var roleid = $.cookie('userrole');
    function cannotrelative(){
        if(roleid == 9 || roleid == 0){
            $('.reliableTd').css({'visibility':'visible'});
        }
    }
    cannotrelative()

    //修改需求来源
    var goEditSource = $('#goEditSource');
    var searchBox = $('#searchBox');
    var existSourceBox = $('#existSourceBox');
    var giveUp = $('#giveUp');
    var goSearchBtn = $('#goSearchBtn');
    var keyMatchSelect = $('#keyMatchSelect');
    var needSource = $('#needSource');
    goEditSource.on('click',function () {
        searchBox.css({'display':'block'})
        existSourceBox.css({'display':'none'})
    })
    giveUp.on('click',function () {
        searchBox.css({'display':'none'});
        existSourceBox.css({'display':'block'});
        needSource.val(originalProjectId);
        _API.getComeSource('needSide',originalCustomerId,false,originalProjectId)
    })
    goSearchBtn.on('click',function () {
        var _val = $(this).siblings('input').val();
        if(_val.length < 2) return;
        _API.checkProNameList('keyMatchSelect',0,true,_val)
    })
    keyMatchSelect.on('change',function () {
        needSource.val($(this).val());
    })

    // 上传文件
    var attachment = new attachment();
    attachment.init({
        fileUpload:$('#fileUpload'),
        addAttachmentList:$('#addAttachmentList')
    }).deleteFile();

    updata();
    function updata(){
        var url  = _ip+'/original/getRequireById?callback=update&id='+_id;
        $.ajax({
            url:url,
            type: 'GET',
            dataType: 'jsonp',
            contentType:'application/json',
            jsonpCallback: 'update',
            success:function(data){
                if(data.success){
                    var data = data.data;
                    $('#document-name').append('ID.'+data.id)
                    _API.getComeSource('needSide',data.customerId,false,data.projectId)
                        .getPlatformList('platformList',data.plat)
                        .getFunctionList('functionList',data.feature,false,false,data.product)
                        .getTypeList('typeList',data.type)
                        .getLevelList('levelList',data.level)
                        .getReliableSet('reliableList',data.reliableDesc)
                        .getProductList('productList',data.product);
                    window.originalProjectId = data.projectId;
                    window.originalCustomerId = data.customerId;
                    $('#originalSourceName').html(data.projectName);
                    $('#needSource').val(data.projectId);
                    $('#needName').val(data.name);
                    $('#scenario').html(data.scenario);
                    $('#nowSolution').html(data.nowSolution);
                    $('#sugSolution').html(data.sugSolution);
                    data.attachment && attachment.getAttachmentList(data.attachment.split(','));  
                }else{
                    alert(data.message);
                }
            }
        })
    }

    var needName = $('#needName');
        scenario = $('#scenario');
    needName.focus(function(){
        needName.removeClass('error-i');
    })
    scenario.focus(function(){
        scenario.removeClass('error-i');
    })

    // 提交编辑
    function commitEdit() {
        if(needName.val() == ''){
            needName.addClass('error-i');
            return
        } 
        if( scenario.val() == ''){
            scenario.addClass('error-i');
            return;
        }

        var attachment = [];
        var li = $('#addAttachmentList').find('li');
        if(li.length >= 1){
            li.each(function(){
                attachment.push( $(this).attr('rel') );
            })
        }
        if($('#needSource').find('option:selected').text() == '请选择'){
            alert('请选择来源');
            return false
        }
        if($('#productList').find('option:selected').text() == '请选择'){
            alert('请选择产品！');
            return false
        }
        if($('#needSide').find('option:selected').text() == '请选择'){
            alert('请选择需求方');
            return false
        }
        var url  = _ip+'/original/updateRequire';
        var data = {
            "id":_id,
            "customerId":$('#needSource').val(),
            "feature":$('#functionList').val(),
            "name":$('#needName').val(),
            "nowSolution":$('#nowSolution').val() || null,
            "plat": $('#platformList').val(),
            "product": $('#productList').val(),
            "projectId":$('#needSource').val(),
            "scenario": $('#scenario').val(),
            "customerId":$('#needSide').val(),
            "sugSolution": $('#sugSolution').val() || null,
            "type": $('#typeList').val(),
            "attachment":attachment.join(','),
            "level":$('#levelList').val(),
            "reliableDesc":$('#reliableList').val()
        }
        $.ajax({
            url:url,
            type : "POST",
            xhrFields: {
                withCredentials: true
            },
            crossDomain: true,
            dataType: 'json',
            contentType:'application/json',
            data:JSON.stringify(data),
            success:function(data){
                if(data.message === 'LOGIN') {
                    window.location.href = 'user-login.html';
                    return false;
                }
                if(data.success){
                    location.href = 'pm-need-detail-mems.html?id='+_id;
                }else{
                    alert(data.message);
                }
            }
        })
   }


    function creatShare(){
        alert('您正在进行编辑，不能进行其他操作！')
    }

     getRelativList();
    var roleid = $.cookie('userrole');
    function getRelativList(){
        var url  = _ip+'/requireRelation/getRequireRelationByOid?oid='+_id;
        $.ajax({
            url:url,
            type: 'GET',
            dataType: 'jsonp',
            contentType:'application/json',
            jsonpCallback: 'off',
            success:function(data){
                var data = data.data;
                var h = '';
                if(!data) {
                    $('#relativeList').html('<tr><td colspan="6" style="text-align: center">暂无数据！</td></tr>');
                    return false;
                }
                for(var k = 0; k < data.length; k++){
                    h += '<tr><td>'+data[k].productName+'</td>';
                        h += '<td>'+data[k].feature+'</td>';
                        h += '<td>'+data[k].pName+'</td>';
                        h += '<td>'+data[k].priority+'</td>';
                        h += '<td>'+data[k].status+'</td>';
                        if(roleid == 1 || roleid == 2 || roleid == 3){
                            h += '<td></td></tr>';
                        }else if(roleid ==0 || roleid == 9){
                            h += '<td><a class="delete" onclick="callOff('+data[k].pId+','+data[k].oId+')">解绑</a></td></tr>';
                        }

                }
                $('#relativeList').html(h);
            }
        })
    }
    function callOff(pid,oid){
        if(confirm('确认解绑？')){
            var url  = _ip+'/requireRelation/deleteRequireRelation?pid='+pid+'&oid='+oid;
            $.ajax({
                url:url,
                type: 'GET',
                dataType: 'jsonp',
                contentType:'application/json',
                jsonpCallback: 'off',
                success:function(data){
                    if (data.success) {
                        location.reload();
                    }
                }
            })
        }

    }

</script>
</body>
</html>